﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using ACompany.OrderProcessing.Presentation.Product;

public partial class Product_ProductDetail : UserControl , IProductDetailView
{

    private ProductPresenter _Presenter;
    protected void Page_Init(object sender, System.EventArgs e)
    {
        _Presenter = new ProductPresenter(this);
    }

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            _Presenter.Display();
        }
    }

    public string Name
    {
        get { return this.txtName.Text; }
        set { this.txtName.Text = value; }
    }

    public decimal Price
    {
        get
        {
            decimal decPrice;
            decimal.TryParse(this.txtPrice.Text, out decPrice);
            return decPrice;
        }
        set { this.txtPrice.Text = value.ToString(); }
    }

    public int Stock
    {
        get
        {
            int intStock;
            Int32.TryParse(this.txtStock.Text, out intStock);
            return intStock;
        }
        set { this.txtStock.Text = value.ToString(); }
    }

    public void ErrorMessage(string Message)
    {
        if (string.IsNullOrEmpty(Message))
        {
            pnlError.Visible = false;
        }
        else
        {
            pnlError.Visible = true;
        }
        this.litErrorMessage.Text = Message.Replace(Environment.NewLine, "<br/>");
    }

    public void InfoMessage(string Message)
    {
        if (string.IsNullOrEmpty(Message))
        {
            pnlInfo.Visible = false;
        }
        else
        {
            pnlInfo.Visible = true;
        }
        this.litInfoMessage.Text = Message.Replace(Environment.NewLine, "<br/>");
    }

    public void ViewTitle(string title)
    {
        this.litTitle.Text = title;
    }

    protected void btnSaveProduct_Click(object sender, System.EventArgs e)
    {
        _Presenter.SaveProduct();
    }

    public Guid ProductId
    {
        get
        {
            Guid SelectedProductId;

            if (string.IsNullOrEmpty(Request.QueryString["ProductId"]))
            {
                SelectedProductId = Guid.Empty;
            }
            else
            {
                SelectedProductId = new Guid(Request.QueryString["ProductId"]);
            }

            return SelectedProductId;
        }
    }

    public string SaveButtonText
    {
        set { this.btnSaveProduct.Text = value; }
    }

    protected void btnCancel_Click(object sender, System.EventArgs e)
    {
        Response.Redirect("ProductList.aspx");
    }

    
}